import { _decorator, Component, find, Node, randomRangeInt, Vec3 } from 'cc';
import { EJoyStickState, JoyStick } from './JoyStick';
import { EplayerAnimation, Player } from './Player';
import { BattleCamera } from './BattleCamera';
import { SkillButton } from './SkillButton';
import { Context } from './Context';
import { Core } from '../core/Core';
import { EGameEvent, EprefabName } from '../core/Config';
import { Monster } from './Monster';
import { ProgressBar } from './ProgressBar';
const { ccclass, property } = _decorator;
/**
 * 战斗场景
 */
@ccclass('Battle')
export class Battle extends Component {

    //摇杆
    @property(Node)
    joyStick: Node;
    //刀光技能按钮
    @property(Node)
    skillButtonAttack: Node;
    //能量球技能按钮
    @property(Node)
    skillButtonEnergyBall: Node;
    //闪现技能按钮
    @property(Node)
    skillButtonSlash: Node;
    //弓箭技能按钮
    @property(Node)
    skillButtonArrow: Node;
    //生命条
    @property(Node)
    lifePreogress: Node;
    //游戏结束
    @property(Node)
    endView: Node;

    isRunning: boolean = false;


    protected onEnable(): void {
        Context.player = this.node.getChildByName('Player').getComponent(Player);
        Context.weapons = this.node.getChildByName('Weapons');
        Context.monsters = this.node.getChildByName('Monsters');
        Context.texts = this.node.getChildByName('Texts');
        Context.battleCamera.followingTarget = Context.player.node;

        Core.instance.event.on(EGameEvent.PLAYER_HURT,this.onPlayerHurt,this);
        Core.instance.event.on(EGameEvent.PLAYER_DEAD,this.onPlayerDead,this);

        this.joyStick.getComponent(JoyStick).onJoyStick((event: EJoyStickState, direction?: Vec3) => {
            switch (event) {
                case EJoyStickState.START:
                    console.log('start');
                    Context.player.setAnimation(EplayerAnimation.WALK);
                    break;
                case EJoyStickState.MOVE:
                    console.log(direction);
                    Context.player.direction.set(direction);
                    break;
                case EJoyStickState.END:
                    console.log('end');
                    Context.player.setAnimation(EplayerAnimation.IDLE);
                    break;
                default:
                    break;
            }
        });
        let attackSkill = this.skillButtonAttack.getComponent(SkillButton);
        attackSkill.coldTime = 0.2;
        attackSkill.onClick(() => Context.player.attack());

        let energyball = this.skillButtonEnergyBall.getComponent(SkillButton);
        energyball.coldTime = 3;
        energyball.onClick(() => Context.player.castEnergyBall());

        let slash = this.skillButtonSlash.getComponent(SkillButton);
        slash.coldTime = 3;
        slash.onClick(() => Context.player.castSlash());

        let arrow = this.skillButtonArrow.getComponent(SkillButton);
        arrow.coldTime = 3;
        arrow.onClick(() => Context.player.castArrow());

        this.lifePreogress.getComponent(ProgressBar).setProgress(1);
        this.startSpawnMonster();
        this.endView.active = false;
        this.isRunning=true;
    }
    protected onDisable(): void {
        Core.instance.event.off(EGameEvent.PLAYER_HURT,this.onPlayerHurt,this);
        Core.instance.event.off(EGameEvent.PLAYER_DEAD,this.onPlayerDead,this);
    }

    start() {

    }

    update(deltaTime: number) {
        if(!this.isRunning){
            return;
        }
        if (Context.monsters.children.length < 20) {
            this.startSpawnMonster();
        }
    }

    private onPlayerHurt(args:any[]) {
        let value = args[0] as number;
        let rate=Context.player.life/Core.instance.playerData.hp;
        this.lifePreogress.getComponent(ProgressBar).setProgress(rate);
        
    }

    private onPlayerDead() {
        this.endView.active = true;
        this.isRunning=false;
        Context.monsters.destroyAllChildren();
    }
    // 开始生成怪物
    private startSpawnMonster() {
        // 生成5个蜘蛛
        for (let i = 0; i < 5; i++) {
            // 创建蜘蛛节点
            let spider = Core.instance.creatNode(EprefabName.SPIDER, Context.monsters);
            // 设置蜘蛛节点的位置
            spider.setPosition(randomRangeInt(-1000, 1000), randomRangeInt(-1000, 1000));

            // 获取蜘蛛节点的Monster组件
            let monster = spider.getComponent(Monster);
            // 设置蜘蛛的速度
            monster.speed = 1.5;
            // 设置蜘蛛是否在移动
            monster.isMoving = true;
        }
        // 生成5个幽灵
        for (let i = 0; i < 5; i++) {
            // 创建幽灵节点
            let ghost = Core.instance.creatNode(EprefabName.GHOST, Context.monsters);
            // 设置幽灵节点的位置
            ghost.setPosition(randomRangeInt(-1000, 1000), randomRangeInt(-1000, 1000));

            // 获取幽灵节点的Monster组件
            let monster = ghost.getComponent(Monster);
            // 设置幽灵的速度
            monster.speed = 0.7;
            // 设置幽灵是否在移动
            monster.isMoving = true;
        }
    }
}


